<template>
  <div>
    <div style="margin-bottom: 5px;">
      <el-input v-model="name" placeholder="请输入球员名" suffix-icon="el-icon-search" style="width: 200px;margin-left: 5px;"
                @keyup.enter.native="loadPost"></el-input>
      <el-select v-model="team" placeholder="请选择球队" style="margin-left: 5px">
        <el-option
            v-for="item in teamData"
            :key="item.id"
            :label="item.name"
            :value="item.id">
        </el-option>
      </el-select>
      <el-select v-model="groups" placeholder="请选择小组" style="margin-left: 5px">
        <el-option
            v-for="item in groupsData"
            :key="item.id"
            :label="item.name"
            :value="item.id">
        </el-option>
      </el-select>
      <el-button type="primary" style="margin-left: 8px;" @click="loadPost">查询</el-button>
      <el-button type="success" @click="resetParam">重置</el-button>
      <el-button type="primary" style="margin-left: 5px;" @click="add">新增</el-button>
    </div>
    <el-table :data="tableData"
              :header-cell-style="{ background: '#ccd0d9', color: '#211313' }"
              border="true"
    >
      <el-table-column prop="id" label="ID" width="60">
      </el-table-column>
      <el-table-column prop="name" label="球员名" width="120">
      </el-table-column>
      <el-table-column prop="team" label="球队名" width="120" :formatter="formatTeam">
      </el-table-column>
      <el-table-column prop="groups" label="小组名" width="120" :formatter="formatGroups">
      </el-table-column>
      <el-table-column prop="age" label="年龄" width="120">
      </el-table-column>
      <el-table-column prop="remark" label="备注" width="600">
      </el-table-column>
      <el-table-column prop="operate" label="操作" width="200">
        <template v-slot="scope">
          <el-button size="small" type="success" @click="mod(scope.row)">编辑</el-button>
          <el-popconfirm
              title="确定删除吗？"
              @confirm="del(scope.row.id)"
              style="margin-left: 5px">
            <el-button slot="reference" size="small" type="danger" >删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[ 5, 10, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>

    <el-dialog
        title="提示"
        :visible.sync="centerDialogVisible"
        width="30%"
        center>

      <el-form ref="form" :rules="rules" :model="form" label-width="80px">
        <el-form-item label="球员名"  prop="name">
          <el-col :span="20">
            <el-input v-model="form.name"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="球队名"  prop="team">
          <el-col :span="20">
            <el-select v-model="form.team" placeholder="请选择球队" style="margin-left: 5px">
              <el-option
                  v-for="item in teamData"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
              </el-option>
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="小组名"  prop="groups">
          <el-col :span="20">
            <el-select v-model="form.groups" placeholder="请选择小组" style="margin-left: 5px">
              <el-option
                  v-for="item in groupsData"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
              </el-option>
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="年龄"  prop="age">
          <el-col :span="20">
            <el-input v-model="form.age"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="备注"  prop="remark">
          <el-col :span="20">
            <el-input type="textarea" v-model="form.remark"></el-input>
          </el-col>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
    <el-button @click="centerDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="save">确 定</el-button>
  </span>
    </el-dialog>

  </div>
</template>
<script>
export default {
  name: "PlayerManage",
  data() {

    return {
      groupsData:[],
      teamData:[],
      tableData: [],
      pageSize:5,
      pageNum:1,
      total:0,
      name:'',
      team: '',
      groups: '',
      centerDialogVisible:false,
      form:{
        id:'',
        name:'',
        team:'',
        groups:'',
        age:'',
        remark:'',
      },
      rules: {
        name: [
          { required: true, message: '请输入球员名', trigger: 'blur' }
        ],
        team: [
          { required: true, message: '请选择球队', trigger: 'blur' }
        ],
        group: [
          { required: true, message: '请选择小组', trigger: 'blur' }
        ]
      },

    }
  },
  methods:{
    formatTeam(row){
      let temp= this.teamData.find(item=>{
        return item.id=row.team
      })
      return temp && temp.name
    },

    formatGroups(row){
      let temp= this.groupsData.find(item=>{
        return item.id=row.groups
      })
      return temp && temp.name
    },

    resetForm(){
      this.$refs.form.resetFields();
    },
    del(id){
      console.log(id);
      this.$axios.get(this.$httpUrl+"/player/del?id="+id).then(res=>res.data).then(res=>{
        console.log(res)
        if(res.code==200){
          this.$message({
            message: '操作成功',
            type: 'success'
          });
          this.loadPost()//重新加载一下数据
        }else{
          this.$message.error('操作失败');
        }
      })
    },
    mod(row){
      this.centerDialogVisible=true
      this.$nextTick(()=>{
        //赋值到表单
        this.form.id=row.id
        this.form.name=row.name
        this.form.team=row.team
        this.form.groups=row.groups
        this.form.age=row.age
        this.form.remark=row.remark
      })
    },
    add(){
      this.centerDialogVisible=true
      this.$nextTick(()=>{
        this.resetForm()
      })
    },
    doSave(){
      this.$axios.post(this.$httpUrl+"/player/save",this.form).then(res=>res.data).then(res=>{
        console.log(res)
        if(res.code==200){
          this.$message({
            message: '操作成功',
            type: 'success'
          });
          this.centerDialogVisible=false
          this.loadPost()//重新加载一下数据
          this.resetForm()
        }else{
          this.$message.error('操作失败');
        }
      })
    },

    doMod(){
      this.$axios.post(this.$httpUrl+"player/update",this.form).then(res=>res.data).then(res=>{
        console.log(res)
        if(res.code==200){
          this.$message({
            message: '操作成功',
            type: 'success'
          });
          this.centerDialogVisible=false
          this.loadPost()//重新加载一下数据
          this.resetForm()
        }else{
          this.$message.error('操作失败');
        }
      })
    },

    save(){
      this.$axios.post(this.$httpUrl+"/player/save",this.form).then(res=>res.data).then(res=>{
        console.log(res)
        if(res.code==200){
          this.$message({
            message: '操作成功',
            type: 'success'
          });
          this.centerDialogVisible=false
          this.loadPost()//重新加载一下数据
          this.resetForm()
        }else{
          this.$message.error('操作失败');
        }
      })
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageNum=1
      this.pageSize=val
      this.loadPost()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum=val
      this.loadPost()
    },

    resetParam(){
      this.name=''
      this.team=''
      this.groups=''
    },
    loadPost(){
      this.$axios.post(this.$httpUrl+"/player/listPageR",{
        pageSize:this.pageSize,
        pageNum:this.pageNum,
        param:{
          name:this.name,
          team: this.team+'',
          groups:this.groups+''
        }
      }).then(res=>res.data).then(res=>{
        console.log(res)
        if(res.code==200){
          this.tableData=res.data
          this.total=res.total
        }else{
          alert("数据获取失败")
        }
      })
    },
    loadTeam(){
      this.$axios.get(this.$httpUrl+"/team/list",).then(res=>res.data).then(res=>{
        console.log(res)
        if(res.code==200){
          this.teamData=res.data
        }else{
          alert("数据获取失败")
        }
      })
    },
    loadGroups(){
      this.$axios.get(this.$httpUrl+"/groups/list",).then(res=>res.data).then(res=>{
        console.log(res)
        if(res.code==200){
          this.groupsData=res.data
        }else{
          alert("数据获取失败")
        }
      })
    },
  },
  beforeMount() {
    this.loadTeam()
    this.loadGroups()
    this.loadPost()

  }

}
</script>

<style scoped>

</style>